<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>HashMap、TreeMap、HashTable、ConcurrentHashMap对比(jdk1.8) | Eetal&#39;s Blog</title>

  <!-- keywords -->
  

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="欢迎查看Eetal的第十篇博客–HashMap、TreeMap、HashTable、ConcurrentHashMap对比(jdk1.8) HashMapHashMap是java中经常用来存取键值对形式的一个集合类1.8以后实现方式为    数组(Node&amp;lt;K,V&amp;gt;[])+链表(Node)首先计算出要存储的key应该到数组中哪个位置的链表下1234static final int ha">
<meta property="og:type" content="article">
<meta property="og:title" content="HashMap、TreeMap、HashTable、ConcurrentHashMap对比(jdk1.8)">
<meta property="og:url" content="http://eetal.gitee.io/blog/2019/03/23/HashMap、TreeMap、HashTable、ConcurrentHashMap对比/index.html">
<meta property="og:site_name" content="Eetal&#39;s Blog">
<meta property="og:description" content="欢迎查看Eetal的第十篇博客–HashMap、TreeMap、HashTable、ConcurrentHashMap对比(jdk1.8) HashMapHashMap是java中经常用来存取键值对形式的一个集合类1.8以后实现方式为    数组(Node&amp;lt;K,V&amp;gt;[])+链表(Node)首先计算出要存储的key应该到数组中哪个位置的链表下1234static final int ha">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2019-03-26T00:32:20.543Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="HashMap、TreeMap、HashTable、ConcurrentHashMap对比(jdk1.8)">
<meta name="twitter:description" content="欢迎查看Eetal的第十篇博客–HashMap、TreeMap、HashTable、ConcurrentHashMap对比(jdk1.8) HashMapHashMap是java中经常用来存取键值对形式的一个集合类1.8以后实现方式为    数组(Node&amp;lt;K,V&amp;gt;[])+链表(Node)首先计算出要存储的key应该到数组中哪个位置的链表下1234static final int ha">
  
    <link rel="alternative" href="/atom.xml" title="Eetal&#39;s Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="http://www.yangyitao.top/FiveChess/resource/ico/favicon.ico">
  
  <link rel="stylesheet" href="/blog/css/style.css">
  
  

  <script src="//cdn.bootcss.com/require.js/2.3.2/require.min.js"></script>
  <script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>

  
</head>
<body>
  <div id="container">
    <div id="particles-js"></div>
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
	<header id="header" class="inner">
		<a href="/" class="profilepic">
			
			<img lazy-src="/blog/images/head.jpg" class="js-avatar">
			
		</a>

		<hgroup>
		  <h1 class="header-author"><a href="/">Eetal</a></h1>
		</hgroup>

		
		<p class="header-subtitle">java coder</p>
		

		
			<div class="switch-btn">
				<div class="icon">
					<div class="icon-ctn">
						<div class="icon-wrap icon-house" data-idx="0">
							<div class="birdhouse"></div>
							<div class="birdhouse_holes"></div>
						</div>
						<div class="icon-wrap icon-ribbon hide" data-idx="1">
							<div class="ribbon"></div>
						</div>
						
						<div class="icon-wrap icon-link hide" data-idx="2">
							<div class="loopback_l"></div>
							<div class="loopback_r"></div>
						</div>
						
						
					</div>
					
				</div>
				<div class="tips-box hide">
					<div class="tips-arrow"></div>
					<ul class="tips-inner">
						<li>菜单</li>
						<li>标签</li>
						
						<li>友情链接</li>
						
						
					</ul>
				</div>
			</div>
		

		<div class="switch-area">
			<div class="switch-wrap">
				<section class="switch-part switch-part1">
					<nav class="header-menu">
						<ul>
						
							<li><a href="/blog/">Home</a></li>
				        
							<li><a href="/blog/archives">Archives</a></li>
				        
						</ul>
					</nav>
					<nav class="header-nav">
						<div class="social">
							
						</div>
					</nav>
				</section>
				
				
				<section class="switch-part switch-part2">
					<div class="widget tagcloud" id="js-tagcloud">
						
					</div>
				</section>
				
				
				
				<section class="switch-part switch-part3">
					<div id="js-friends">
					
			          <a target="_blank" class="main-nav-link switch-friends-link" href="https://github.com/smackgg/hexo-theme-smackdown">smackdown</a>
			        
			        </div>
				</section>
				

				
			</div>
		</div>
	</header>				
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
  	<div class="overlay">
  		<div class="slider-trigger"></div>
  		<h1 class="header-author js-mobile-header hide">Eetal</h1>
  	</div>
	<div class="intrude-less">
		<header id="header" class="inner">
			<div class="profilepic">
				<img lazy-src="/blog/images/head.jpg" class="js-avatar">
			</div>
			<hgroup>
			  <h1 class="header-author">Eetal</h1>
			</hgroup>
			
			<p class="header-subtitle">java coder</p>
			
			<nav class="header-menu">
				<ul>
				
					<li><a href="/blog/">Home</a></li>
		        
					<li><a href="/blog/archives">Archives</a></li>
		        
		        <div class="clearfix"></div>
				</ul>
			</nav>
			<nav class="header-nav">
				<div class="social">
					
				</div>
			</nav>
		</header>				
	</div>
</nav>
      <div class="body-wrap"><article id="post-HashMap、TreeMap、HashTable、ConcurrentHashMap对比" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/blog/2019/03/23/HashMap、TreeMap、HashTable、ConcurrentHashMap对比/" class="article-date">
  	<time datetime="2019-03-23T07:29:54.608Z" itemprop="datePublished">2019-03-23</time>
</a>
    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      HashMap、TreeMap、HashTable、ConcurrentHashMap对比(jdk1.8)
      
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
        

        
        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>欢迎查看Eetal的第十篇博客–HashMap、TreeMap、HashTable、ConcurrentHashMap对比(jdk1.8)</p>
<h2 id="HashMap"><a href="#HashMap" class="headerlink" title="HashMap"></a>HashMap</h2><p>HashMap是java中经常用来存取键值对形式的一个集合类<br>1.8以后实现方式为    数组(Node&lt;K,V&gt;[])+链表(Node)<br>首先计算出要存储的key应该到数组中哪个位置的链表下<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">static final int <span class="built_in">hash</span>(Object key) &#123;</span><br><span class="line">       int h;</span><br><span class="line">       <span class="built_in">return</span> (key == null) ? 0 : (h = key.hashCode()) ^ (h &gt;&gt;&gt; 16);</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure></p>
<p>静态方法hash将key的hashCode的高16位与低16位异或的值作为其hash值,异或是为了减少在map的hash数组长度较小时的hash碰撞，使其更均匀<br>如果key是null则为0，这也是为什么HashMap可以使用null作为key而ConcurrentHashMap不行的原因<br>在putVal方法中使用这个hash的值去与当前的数组大小求余计算出其位置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">final V putVal(int <span class="built_in">hash</span>, K key, V value, boolean onlyIfAbsent,</span><br><span class="line">				   boolean evict) &#123;</span><br><span class="line">		Node&lt;K,V&gt;[] tab; Node&lt;K,V&gt; p; int n, i;</span><br><span class="line">		<span class="keyword">if</span> ((tab = table) == null || (n = tab.length) == 0)</span><br><span class="line">			n = (tab = resize()).length;</span><br><span class="line">		<span class="keyword">if</span> ((p = tab[i = (n - 1) &amp; <span class="built_in">hash</span>]) == null) //---计算数组位置</span><br><span class="line">			tab[i] = newNode(<span class="built_in">hash</span>, key, value, null);</span><br><span class="line">		<span class="keyword">else</span> &#123;</span><br><span class="line">			Node&lt;K,V&gt; e; K k;</span><br><span class="line">			<span class="keyword">if</span> (p.hash == <span class="built_in">hash</span> &amp;&amp;</span><br><span class="line">				((k = p.key) == key || (key != null &amp;&amp; key.equals(k))))</span><br><span class="line">				e = p;</span><br><span class="line">			<span class="keyword">else</span> <span class="keyword">if</span> (p instanceof TreeNode)</span><br><span class="line">				e = ((TreeNode&lt;K,V&gt;)p).putTreeVal(this, tab, <span class="built_in">hash</span>, key, value);</span><br><span class="line">			<span class="keyword">else</span> &#123;</span><br><span class="line">				<span class="keyword">for</span> (int binCount = 0; ; ++binCount) &#123;</span><br><span class="line">					<span class="keyword">if</span> ((e = p.next) == null) &#123;</span><br><span class="line">						p.next = newNode(<span class="built_in">hash</span>, key, value, null);</span><br><span class="line">						<span class="keyword">if</span> (binCount &gt;= TREEIFY_THRESHOLD - 1) // -1 <span class="keyword">for</span> 1st</span><br><span class="line">							treeifyBin(tab, <span class="built_in">hash</span>);</span><br><span class="line">						<span class="built_in">break</span>;</span><br><span class="line">					&#125;</span><br><span class="line">					<span class="keyword">if</span> (e.hash == <span class="built_in">hash</span> &amp;&amp;</span><br><span class="line">						((k = e.key) == key || (key != null &amp;&amp; key.equals(k))))</span><br><span class="line">						<span class="built_in">break</span>;</span><br><span class="line">					p = e;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">if</span> (e != null) &#123; // existing mapping <span class="keyword">for</span> key</span><br><span class="line">				V oldValue = e.value;</span><br><span class="line">				<span class="keyword">if</span> (!onlyIfAbsent || oldValue == null)</span><br><span class="line">					e.value = value;</span><br><span class="line">				afterNodeAccess(e);</span><br><span class="line">				<span class="built_in">return</span> oldValue;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		++modCount;</span><br><span class="line">		<span class="keyword">if</span> (++size &gt; threshold)</span><br><span class="line">			resize();</span><br><span class="line">		afterNodeInsertion(evict);</span><br><span class="line">		<span class="built_in">return</span> null;</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
<h2 id="重要参数"><a href="#重要参数" class="headerlink" title="重要参数"></a>重要参数</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">static final int DEFAULT_INITIAL_CAPACITY = 1 &lt;&lt; 4; // aka 16</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * The maximum capacity, used <span class="keyword">if</span> a higher value is implicitly specified</span><br><span class="line"> * by either of the constructors with arguments.</span><br><span class="line"> * MUST be a power of two &lt;= 1&lt;&lt;30.</span><br><span class="line"> */</span><br><span class="line">static final int MAXIMUM_CAPACITY = 1 &lt;&lt; 30;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * The load factor used when none specified <span class="keyword">in</span> constructor.</span><br><span class="line"> */</span><br><span class="line">static final <span class="built_in">float</span> DEFAULT_LOAD_FACTOR = 0.75f;</span><br></pre></td></tr></table></figure>
<h2 id="重要构造函数"><a href="#重要构造函数" class="headerlink" title="重要构造函数"></a>重要构造函数</h2><p>hashmap限制容量CAPACITY为2的幂，每次扩容为乘以2,这样可以保证数据的均匀分布，同时使得hashmap只有resize方法而不需要rehash,下面详谈<br>第一个DEFAULT_INITIAL_CAPACITY默认的数组大小，也就是不指定时默认开辟的hash数组大小为2的4次方<br>第二个MAXIMUM_CAPACITY，最大扩容至的hash数组大小,如果指定的数组大小大于这个值，会被替换为这个值,原因是,数组的下表要求是int类型,而int在java中是4个字节,共32位，而第一位表示符号位，因此可以表示的最大2的幂为2的30次方<br>最后DEFAULT_LOAD_FACTOR代表负载因子,当put操作以后 map的size &gt; hash数组的长度*负载因子时,就会触发扩容，<br>当然hashmap提供了指定initialCapacity的构造函数,但在1.8里，这个参数并不会改变其capacity，只会改变threshold的值以下为源码<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">    * Constructs an empty &lt;tt&gt;HashMap&lt;/tt&gt; with the specified initial</span><br><span class="line">    * capacity and load factor.</span><br><span class="line">    *</span><br><span class="line">    * @param  initialCapacity the initial capacity</span><br><span class="line">    * @param  loadFactor      the load factor</span><br><span class="line">    * @throws IllegalArgumentException <span class="keyword">if</span> the initial capacity is negative</span><br><span class="line">    *         or the load factor is nonpositive</span><br><span class="line">    */</span><br><span class="line">   public HashMap(int initialCapacity, <span class="built_in">float</span> loadFactor) &#123;</span><br><span class="line">       <span class="keyword">if</span> (initialCapacity &lt; 0)</span><br><span class="line">           throw new IllegalArgumentException(<span class="string">"Illegal initial capacity: "</span> +</span><br><span class="line">                                              initialCapacity);</span><br><span class="line">       <span class="keyword">if</span> (initialCapacity &gt; MAXIMUM_CAPACITY)</span><br><span class="line">           initialCapacity = MAXIMUM_CAPACITY;</span><br><span class="line">       <span class="keyword">if</span> (loadFactor &lt;= 0 || Float.isNaN(loadFactor))</span><br><span class="line">           throw new IllegalArgumentException(<span class="string">"Illegal load factor: "</span> +</span><br><span class="line">                                              loadFactor);</span><br><span class="line">       this.loadFactor = loadFactor;</span><br><span class="line">       this.threshold = tableSizeFor(initialCapacity);</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   /**</span><br><span class="line">    * Returns a power of two size <span class="keyword">for</span> the given target capacity.</span><br><span class="line">    */</span><br><span class="line">   static final int tableSizeFor(int <span class="built_in">cap</span>) &#123;</span><br><span class="line">       int n = <span class="built_in">cap</span> - 1;</span><br><span class="line">       n |= n &gt;&gt;&gt; 1;</span><br><span class="line">       n |= n &gt;&gt;&gt; 2;</span><br><span class="line">       n |= n &gt;&gt;&gt; 4;</span><br><span class="line">       n |= n &gt;&gt;&gt; 8;</span><br><span class="line">       n |= n &gt;&gt;&gt; 16;</span><br><span class="line">       <span class="built_in">return</span> (n &lt; 0) ? 1 : (n &gt;= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure></p>
<p>指定initialCapacity只能改变扩容时机，而并不改变hash数组长度</p>
<h2 id="put操作与resize"><a href="#put操作与resize" class="headerlink" title="put操作与resize"></a>put操作与resize</h2><p>put操作较简单，计算到对应Node,为null则新建,不为null则根据该Node类型为TreeNode还是普通Node执行不同的插入操作<br>当put以后，如果size大于threshold扩容时机，则进行扩容（数组长度乘以2）<br>同时进行rehash,此处源码使用了一个快捷的办法，因为数组的长度必定为2的倍数<br>假设长度为2的x次方,则旧hash数组第i个位置上的链表的所有Node的hash都应该为 Nj<em>(2的x次方)+i<br>因为新扩容的数组长度为原来的两倍(重点),则 长度为 2</em>(2的x次方)，此时进行resize<br>hash%(2<em>(2的x次方)) == (hash%(2的x次方))+(2的x次方)</em>(hash&amp;(2的x+1次方))<br>而hash%(2的x次方)为node所在旧hash数组下标i,oldCap为旧数组的长度，式子可写为<br>    新数组的位置下标 newI = i + oldCap * (hash &amp; oldCap)<br>官方实现代码如下,将旧数组每个位置上的链表拆分为高低两条新链表，分别代表hash &amp; oldCap为0和为1的节点<br>再把这两条链表对应到新数组的位置 newTab[j] 和 newTab[oldCap + j]</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">// j from 0 to oldCap-1</span><br><span class="line">Node&lt;K,V&gt; loHead = null, loTail = null;</span><br><span class="line">Node&lt;K,V&gt; hiHead = null, hiTail = null;</span><br><span class="line">Node&lt;K,V&gt; next;</span><br><span class="line"><span class="keyword">do</span> &#123;</span><br><span class="line">	next = e.next;</span><br><span class="line">	<span class="keyword">if</span> ((e.hash &amp; oldCap) == 0) &#123;</span><br><span class="line">		<span class="keyword">if</span> (loTail == null)</span><br><span class="line">			loHead = e;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">			loTail.next = e;</span><br><span class="line">		loTail = e;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		<span class="keyword">if</span> (hiTail == null)</span><br><span class="line">			hiHead = e;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">			hiTail.next = e;</span><br><span class="line">		hiTail = e;</span><br><span class="line">	&#125;</span><br><span class="line">&#125; <span class="keyword">while</span> ((e = next) != null);</span><br><span class="line"><span class="keyword">if</span> (loTail != null) &#123;</span><br><span class="line">	loTail.next = null;</span><br><span class="line">	newTab[j] = loHead;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">if</span> (hiTail != null) &#123;</span><br><span class="line">	hiTail.next = null;</span><br><span class="line">	newTab[j + oldCap] = hiHead;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="HashMap死锁问题"><a href="#HashMap死锁问题" class="headerlink" title="HashMap死锁问题"></a>HashMap死锁问题</h2><p>因为HashMap中的put和remove都是不加对象锁的，也就是非线程安全的<br>且其table属性(hash数组的引用)也没有使用volatile修饰(修饰了也不能百分百解决并发问题,因此官方默认HashMap为非线程安全的map以提高其效率)<br>如果在resize的过程,另一个线程拿到的是oldTable，并对其进行如删除元素或新增元素<br>因为原有的单条链表hi拆分重新组建为高低两条链表，这期间对oldTable节点的插入和删除操作可能会导致把新构建的链表中的部分节点形成环(因为新链表的序列与oldTable原有不一样)<br>最终导致在get和put时，遍历链表时进入无限死循环<br>因此在多线程下应使用ConcurrentHashMap代替HashMap</p>
<h2 id="TreeMap"><a href="#TreeMap" class="headerlink" title="TreeMap"></a>TreeMap</h2><p>TreeMap为有序的map,本质是一条链表<br>可以在构造是传入一个比较器，或者每个元素的类都有实现Comparable接口<br>HashMap的插入为计算到数组位置后,添加到对应Node链表末端<br>TreeMap为比较计算位置后插入对应链表位置<br>在未传入比较器的情况下，key不允许为null</p>
<h2 id="简介ConcurrentHashMap和HashTable"><a href="#简介ConcurrentHashMap和HashTable" class="headerlink" title="简介ConcurrentHashMap和HashTable"></a>简介ConcurrentHashMap和HashTable</h2><p>HashTable为HashMap的线程安全版,其中会有线程安全问题的方法都采用使用this对象作为锁的方式来实现互斥<br>而ConcurrentHashMap1.5以前为使用分段锁，即双数组,原本存放Entry的地方改为存放segemant数组，代码1000多行<br>segemant对象继承了ReentrantLock，包含有一个与Entry数组的属性<br>插入和删除修改时,只锁被Hash到的segemant对象，再hash元素到segemant的Entry数组对应的位置，插入到对应位置链表中去<br>jdk1.5开始，Doug Lea 大牛改为和HashMap一样的Node结构,往类里注入了Unsafe对象<br>修改时只锁被hash到的对应的链表头指针Node，通过各种cas操作和synchronized检验和修改插入等操作<br>jdk1.5以后的ConcurrentHashMap的源码足足来到6312行，TQL，到处都是使用unsafe的cas和计算对象偏移来完成原语操作<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">   // Unsafe mechanics</span><br><span class="line">   private static final sun.misc.Unsafe U;</span><br><span class="line">   private static final long SIZECTL;</span><br><span class="line">   private static final long TRANSFERINDEX;</span><br><span class="line">   private static final long BASECOUNT;</span><br><span class="line">   private static final long CELLSBUSY;</span><br><span class="line">   private static final long CELLVALUE;</span><br><span class="line">   private static final long ABASE;</span><br><span class="line">   private static final int ASHIFT;</span><br><span class="line"></span><br><span class="line">   static &#123;</span><br><span class="line">       try &#123;</span><br><span class="line">           U = sun.misc.Unsafe.getUnsafe();</span><br><span class="line">		...</span><br><span class="line">		&#125;</span><br><span class="line">	...	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>因此效率上而言ConcurrentHashMap会优于HashTable</p>
<h3 id="请移步"><a href="#请移步" class="headerlink" title="请移步"></a>请移步</h3><p>个人主页: <a href="http://yangyitao.top" target="_blank" rel="noopener">yangyitao.top</a></p>

      
    </div>
    
  </div>
  
    
<nav id="article-nav">
  
    <a href="/blog/2019/03/24/并发基石-Markword与锁升级/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">&lt;</strong>
      <div class="article-nav-title">
        
          并发基石-Markword与锁升级
        
      </div>
    </a>
  
  
    <a href="/blog/2019/03/23/使用future机制和对象锁实现SynchronizedExecutor/" id="article-nav-older" class="article-nav-link-wrap">
      <div class="article-nav-title">使用future机制和对象锁实现SynchronizedExecutor</div>
      <strong class="article-nav-caption">&gt;</strong>
    </a>
  
</nav>

  
</article>


<div class="ds-share share" data-thread-key="HashMap、TreeMap、HashTable、ConcurrentHashMap对比" data-title="HashMap、TreeMap、HashTable、ConcurrentHashMap对比(jdk1.8)" data-url="http://eetal.gitee.io/blog/blog/2019/03/23/HashMap、TreeMap、HashTable、ConcurrentHashMap对比/"  data-images="/blog/images/head.jpg" data-content="HashMap、TreeMap、HashTable、ConcurrentHashMap对比(jdk1.8)">
    <div class="ds-share-inline">
      <ul  class="ds-share-icons-16">
      	<li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
        <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
        <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
        <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
        <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>
      </ul>
      <div class="ds-share-icons-more">
      </div>
    </div>
 </div>
 





</div>
      <footer id="footer">
  <div class="outer">
    <div id="footer-info">
      <div class="footer-left">
        &copy; 2019 Eetal
      </div>
        <div class="footer-right">
          <a href="http://hexo.io/" target="_blank">Hexo</a>  Theme <a href="https://github.com/smackgg/hexo-theme-smackdown" target="_blank">Smackdown</a>
        </div>
    </div>
  </div>
</footer>
    </div>
    
  <link rel="stylesheet" href="/blog/fancybox/jquery.fancybox.css">


<script>
	var yiliaConfig = {
		fancybox: true,
		mathjax: true,
		animate: true,
		isHome: false,
		isPost: true,
		isArchive: false,
		isTag: false,
		isCategory: false,
		open_in_new: false
	}
</script>
<script src="/blog/js/main.js"></script>



<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script src="//cdn.bootcss.com/mathjax/2.7.0/MathJax.js"></script>


  </div>
</body>
</html>